% Assumes that groups.dat defines the areas you want to see.
% Must be in the directory in which the simulation was run.
%
% Col #1 is post cell, col#2 is pre cell, col#3 weight, col#4 is delay.
%
% plot_syn_diff_area(area_name, type_name, num_procs, thresh);
function plot_syn_diff_area(area_name, type_name, num_procs, thresh);



connectfile='connect.dat';
[a b c d]=textread(connectfile,'%f%f%f%f%*[^\n]','delimiter','\t');
connect=[a b d]; % x y coord of area & d is cell type index.


read_groups; % Assumes groups.dat has the neural area data

if ~exist('thresh')
    thresh = 0.2;
end

index = [];
for i=1:length(areas)
    if strcmp( deblank(areas{i}), area_name) && strcmp( type_name, deblank(celltype{i})) 
        index = i
        break;
    end
end
if isempty(index)
    disp([area_name ' ' type_name ' is not in groups.dat.']);
    return;
end

%neuron_num=43511+16
neuron_num = neuron_id(index,1):num_procs:neuron_id(index,2);


synfile = [ num2str(mod(neuron_num(1),num_procs)) '-' num2str(neuron_id(end,end)+1) '.syn']
synfile2 = [ num2str(mod(neuron_num(1),num_procs)) '-' num2str(neuron_id(end,end)+1) 'final.syn']

if ~exist(synfile,'file')
    disp([synfile ' does not exist. Exiting.']);
    return;
end
if ~exist(synfile2,'file')
    disp([synfile2 ' does not exist. Exiting.']);
    return;
end

X=load(synfile);
X_final=load(synfile2);

    zlabels = '';
    for i=1:length(areas)
        if strcmp( deblank(areas{i}), area_name) 
            zlabels = [zlabels deblank(celltype{i}) '|'];
        end
    end


for kk=1:length(neuron_num)
    figure(kk)
    ind=find(X(:,1)==neuron_num(kk));

    syn_diff=X_final(ind,3)-X(ind,3);

    length(ind)
    hold off
    for ii=1:length(ind)
        % xyz=[connect(X7_19350(ind(ii),2)+1,[1,2,8]);connect(X7_19350(ind(ii),1)+1,[1,2,8])];
        xyz0=connect(X(ind(ii),2)+1,:);
        if syn_diff(ii)>thresh
            plot3(xyz0(:,1),xyz0(:,2),xyz0(:,3)+1,'r.');
        elseif syn_diff(ii)<-thresh
             plot3(xyz0(:,1),xyz0(:,2),xyz0(:,3)+1,'g.');
        else
            plot3(xyz0(:,1),xyz0(:,2),xyz0(:,3)+1,'y.');
        end
        hold on
    end;
    xyz1=connect(X(ind(1),1)+1,:);
    plot3(xyz1(:,1),xyz1(:,2),xyz1(:,3)+1,'k*');
    
    title(['Neurons connected to cell #: ', num2str(neuron_num(kk)) ' (red >, green <)']);
    set(gca,'zlim',[1 22],'ZTick',[1:22],'ZTickLabel',zlabels);

    pause;
    clear ind
end;





  